package com.xiao.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiao.student.bean.Fangtan;
import com.xiao.student.bean.Renyuan;
import com.xiao.student.bean.User;
import com.xiao.student.service.RenyuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by hz on 2017/1/24.
 */
@Controller("renyuan")
@RequestMapping("/ry")
public class RenyuanController {

    @Autowired
   private RenyuanService ryservice;


    @RequestMapping("/login")
    public String login1(){
        return "login";
    }


    /**
     * 用户登录验证
     * @param user
     * @return
     */
    @RequestMapping("dologin")
    public String login(User user,HttpServletRequest request){
        String msg="";
        String ym="";
        User rs = ryservice.get(user);
        if (rs!=null){
            // TODO: 2017/1/24 跳转至首页
            request.getSession().setAttribute("USER",rs);
            ym="home";
        }else {
           /* msg="用户名或者密码错误";
            request.setAttribute("msg",msg);*/
            ym="login";                 // TODO: 2017/1/24 设置登录页面
        }

        return ym;
    }




    /**
     * 人员添加
     * @param ry
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String ryadd(Renyuan ry){
        String msg="false";
        boolean rs = ryservice.save(ry);
        if (rs){
            msg="success";
        }
        JSONObject json=new JSONObject();
        json.put("msg",msg);
        return json.toJSONString();
    }

    /**
     * 学员取出
     * @return
     */
    @RequestMapping("/xy")
    public ModelAndView xueyuan(){
        List<Renyuan> rs = ryservice.get("学员");
        ModelAndView mv=new ModelAndView();
        mv.addObject("xylist",rs);
        mv.setViewName("xueyuan");
        return mv;
    }


    /**
     * 学生取出
     * @return
     */
    @RequestMapping("/xs")
    public ModelAndView xuesheng(){
        List<Renyuan> rs = ryservice.get("学生");
        ModelAndView mv=new ModelAndView();
        mv.addObject("xslist",rs);
        mv.setViewName("xuesheng");
        return mv;
    }


    /**
     * 更新信息学员与学生
     * @param ry
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public String jybyid(Renyuan ry){
        String msg="false";
        JSONObject json=new JSONObject();
        boolean rs = ryservice.xinxi(ry);
        if (rs){
            msg="success";
        }
        json.put("msg",msg);
        return json.toJSONString();
    }
    /**
     * 学生更新学员
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/xygx")
    public String xsxy(@RequestParam(value = "id") String id){
        String msg="false";
        boolean rs = ryservice.update(id);
        if (rs){
            msg="success";
        }
        JSONObject json=new JSONObject();
        json.put("msg",msg);
        return json.toJSONString();
    }
    /**
     * 导向人员信息录入页面
     */
    @RequestMapping("/addym")
    public ModelAndView addym(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("luru");
        return mv;
    }


    /**
     * 增加访谈
     * @param
     * @return
     */
    @RequestMapping("/getft")
    public ModelAndView getft(String id){
        ModelAndView mv=new ModelAndView();
        List<Fangtan> rs = ryservice.getft(id);
        mv.addObject("ftlist",rs);
        mv.setViewName(""); // TODO: 2017/1/24 添加页面
        return mv;
    }

}
